import BaseTreeSelect from '../../BaseTreeSelect/index.vue'
import {AbstractComponent} from "/@/components/Base/BaseEditTable/component/AbstractComponent";
import {isRef, nextTick} from "vue";

export class SelectTree extends AbstractComponent {
    public async getFocus(vRef: any) {
        await nextTick()
        let timer = setTimeout(() => {
            vRef.value.$el.click()
            clearTimeout(timer);
        }, 1);
    }

    private value
    private options = []
    private props = {
        label: 'label',
        value: 'value',
        children: 'children'
    }

    public setProperties(value, obj) {
        this.value = value
        this.options = isRef(obj.data) ? obj.data.value : obj.data
        if (obj.props) {
            this.props = obj.props
        }
    }


    public getComponent() {
        return BaseTreeSelect
    }

    public formatLabel(): string {
        return this.setLable(this.options,this.value)
    }

    private setLable(list,value): string {
        if(list && list.length > 0){
            for(const item of list){
                if(item[this.props.value] == this.value){
                    return item[this.props.label]
                }
                let children = item[this.props.children]
                if(children && children.length > 0){
                     let label = this.setLable(children,value)
                     if(label){
                        return label
                     }  
                }
            }
        }
        return '';
        
    }

    public whetherFormat(): boolean {
        return true;
    }
}